package com.lee.crawler.gov.strategy;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.springframework.stereotype.Component;

@Component
public class FietArticleStrategy extends AbstractArticleStrategy {
	private static final Pattern timePattern = Pattern.compile("发布时间：(.*)");
	private static final DateTimeFormatter dateTimePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

	@Override
	public String getTitle(Document content) {
		return content.selectFirst(".content h3").text();
	}

	@Override
	public Timestamp getPublishedTime(Document content) {
		String publishedTimeStr = content.selectFirst(".content span:contains(发布时间)").text();
		Matcher matcher = timePattern.matcher(publishedTimeStr);
		if (matcher.find()) {
			String timeStr = matcher.group(1);
			if(timeStr.matches("\\d{4}-\\d{2}-\\d{2}")){
				timeStr=timeStr+" 00:00";
			}
			LocalDateTime localDateTime = LocalDateTime.parse(timeStr,dateTimePattern);
			return Timestamp.valueOf(localDateTime);

		}
		return Timestamp.valueOf(LocalDateTime.now());
	}

	@Override
	public String getContent(Document content) {
		return content.selectFirst(".TRS_Editor,#detailContent").text();
	}

	@Override
	public String getHtmlContent(Document content) {
		Element htmlContentEl = content.selectFirst(".TRS_Editor");
		String htmlContent = Jsoup.clean(htmlContentEl.html(), Whitelist.relaxed());
		return htmlContent;
	}

}
